/**
 * 登录状态管理
 */
import { message } from "ant-design-vue";
import request from "@/axios";
import { setlocalData } from "@/utils/updateLocalData";
import router from "@/router";

export default {
  namespaced: true,
  state: {
    // 当前用户信息
    user: null,
    // token
    token: null,
    // 当前用户的菜单
    menus: null,
  },
  mutations: {
    // 修改值
    SET(state: any, obj: any) {
      setlocalData();
      state[obj.key] = obj.value;
    },
  },
  actions: {
    getMenus({ commit }) {
      return new Promise((resolve, reject) => {
        request
          .getAndLoadData("/adminapi/menu/user", {})
          .then((data: any) => {
            resolve(data);
          })
          .catch((err) => {
            message.error("获取菜单数据出错了!");
            router.push("/404");
          });
      });
    },
    loginOut({ commit, state }) {
      const { username, password } = state.user;
      return new Promise((resolve, reject) => {
        // request.getAndLoadData("/adminapi/logout", {
        //   username,
        //   password,
        // });

        localStorage.removeItem("user");
        message.success("退出成功！");
        resolve(true);
      });
    },
  },
};
